package com.zzl.alligator.management.system.controller;

import com.zzl.alligator.common.enums.BusinessTypeEnum;
import com.zzl.alligator.common.util.ExcelUtils;
import com.zzl.alligator.common.util.SecurityUtils;
import com.zzl.alligator.framework.aspectj.annotation.Log;
import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.system.domain.SysUserAssist;
import com.zzl.alligator.management.system.service.ISysUserAssistService;
import com.zzl.alligator.management.system.vo.NoticeCountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户辅助表(SysUserAssist)控制层
 *
 * @author zhouziliang
 * @since 2020-07-16
 */
@RestController
@RequestMapping("/system/userAssist")
public class SysUserAssistController extends BaseController {
    
    /**
     * 服务对象
     */
    @Autowired
    private ISysUserAssistService sysUserAssistService;

    /**
     * 分页查询所有数据
     *
     * @param sysUserAssist 查询实体
     * @return 所有数据
     */
    @GetMapping
    public JsonResult findList(SysUserAssist sysUserAssist) {
        startPage();
        List<SysUserAssist> sysUserAssistList = sysUserAssistService.findSysUserAssistList(sysUserAssist);
        return JsonResult.success(getDataTable(sysUserAssistList));
    }

    @GetMapping("/profile")
    public JsonResult findUserProfile() {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysUserAssist assist = sysUserAssistService.findSysUserAssistById(userId);
        List<NoticeCountVO> list = new ArrayList<>(2);
        NoticeCountVO vo1 = new NoticeCountVO();
        vo1.setLabel("通知");
        vo1.setCount(assist.getPersonNoticeCount());
        vo1.setTabName("personNotice");
        list.add(vo1);
        NoticeCountVO vo2 = new NoticeCountVO();
        vo2.setLabel("公告");
        vo2.setCount(assist.getPubNoticeCount());
        vo2.setTabName("sysNotice");
        list.add(vo2);
        return JsonResult.success(list);
    }

    @PutMapping("/clearPubNotice/{userId}")
    public JsonResult readAllPubNotice(@PathVariable("userId") Long userId) {
        return toAjax(sysUserAssistService.userPubNoticeClear(userId));
    }
    /**
     * 通过主键查询单条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("/{userId}")
    public JsonResult findById(@PathVariable Long userId) {
        return JsonResult.success(sysUserAssistService.findSysUserAssistById(userId));
    }

    /**
     * 新增数据
     *
     * @param sysUserAssist 实体对象
     * @return 新增结果
     */
    @PostMapping
    @Log(title = "新增用户辅助表", businessType = BusinessTypeEnum.INSERT)
    public JsonResult add(@RequestBody SysUserAssist sysUserAssist) {
        return toAjax(this.sysUserAssistService.insertSysUserAssist(sysUserAssist));
    }

    /**
     * 修改数据
     *
     * @param sysUserAssist 实体对象
     * @return 修改结果
     */
    @PutMapping
    @Log(title = "更新用户辅助表", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult update(@RequestBody SysUserAssist sysUserAssist) {
        return toAjax(this.sysUserAssistService.updateSysUserAssist(sysUserAssist));
    }

    /**
     * 删除数据
     *
     * @param userId 主键
     * @return 删除结果
     */
    @DeleteMapping("/{userId}")
    @Log(title = "删除用户辅助表", businessType = BusinessTypeEnum.DELETE)
    public JsonResult delete(@PathVariable Long userId) {
        return toAjax(this.sysUserAssistService.deleteById(userId));
    }
    
    /**
     * 导出数据
     * 
     * @param sysUserAssist 条件
     * @return 导出文件路径
     */
    @GetMapping("/export")
    @Log(title = "导出用户辅助表", businessType = BusinessTypeEnum.EXPORT)
    public JsonResult export(SysUserAssist sysUserAssist) {
        List<SysUserAssist> list = sysUserAssistService.findSysUserAssistList(sysUserAssist);
        ExcelUtils<SysUserAssist> util = new ExcelUtils<>(SysUserAssist.class);
        return util.exportExcel(list, "用户辅助表数据");
    }
}